package com.asm.handler;

import com.asm.message.GroupQuitRequestMessage;
import com.asm.message.GroupQuitResponseMessage;
import com.asm.server.session.Group;
import com.asm.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @ClassName GroupQuitRequestMessage
 * @author chu
 * @date 2024/7/20
 * @Descript 移除群成员
 * */
@ChannelHandler.Sharable
public class GroupQuitRequestMessageHandler extends SimpleChannelInboundHandler<GroupQuitRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupQuitRequestMessage msg) throws Exception {
       Group group = GroupSessionFactory.getGroupSession().removeMember(msg.getGroupName(),msg.getUsername());
        System.out.println(group);
        if (group!= null) {
            ctx.writeAndFlush(new GroupQuitResponseMessage(true, "退出群聊成功！"));
        }else{
            ctx.writeAndFlush(new GroupQuitResponseMessage(false, "该群聊不存在！"));
        }
    }
}
